﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.DataModel;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class PipeSlope : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			Result result;
			try
			{
				UIDocument activeUIDocument = cmdData.Application.ActiveUIDocument;
				Document document = activeUIDocument.Document;
				if (!YJKRevitTools.ViewCheck(activeUIDocument, new List<ViewType>
				{
					(ViewType)1,
					(ViewType)2,
					(ViewType)4
				}))
				{
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					Transaction transaction = new Transaction(document, "水管坡度调整");
					transaction.Start();
					PipeSlopeForm pipeSlopeForm = new PipeSlopeForm(this.userSetting);
					pipeSlopeForm.ShowDialog();
					if (pipeSlopeForm.DialogResult == DialogResult.OK)
					{
						new HYPipeSlope(pipeSlopeForm.slope / 100.0, pipeSlopeForm.startElevation).MovePipe(activeUIDocument);
					}
					transaction.Commit();
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		private UserSetting userSetting = new UserSetting();
	}
}
